package com.nowcoder.community.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.nowcoder.community.entity.Message;
import com.nowcoder.community.mapper.MessageMapper;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: community
 * @description:
 * @author: hmx
 * @create: 2021-12-19 22:31
 **/
@Service
public class MessageService {

    @Resource
    private MessageMapper messageMapper;

    public List<Message> findConversations(int userId, int offset, int limit) {
        return messageMapper.selectConversations(userId, offset, limit);
    }


    public int findConversationCount(int userId) {
        return messageMapper.selectConversationCount(userId);
    }

    /**
     * 查询某个会话所包含的私信列表.
     * @param conversationId
     * @param offset
     * @param limit
     * @return
     */
    public List<Message> findLetters(String conversationId, int offset, int limit) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Message::getStatus, 2)
                .ne(Message::getFromId, 1)
                .eq(Message::getConversationId, conversationId)
                .orderByDesc(Message::getId)
                .last(" limit " + offset + ", " + limit);
        return messageMapper.selectList(queryWrapper);
    }

    /**
     * 查询某个会话所包含的私信数量.
     * @param conversationId
     * @return
     */
    public int findLetterCount(String conversationId) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Message::getStatus, 2)
                .ne(Message::getFromId, 1)
                .eq(Message::getConversationId, conversationId);
        return messageMapper.selectCount(queryWrapper);
    }

    /**
     * 查询某个用户未读消息的数量
     * @param userId
     * @param conversationId
     * @return
     */
    public int findLetterUnreadCount(int userId, String conversationId) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getStatus, 0)
                .ne(Message::getFromId, 1)
                .eq(Message::getToId, userId);
        if (conversationId != null) {
            queryWrapper.eq(Message::getConversationId, conversationId);
        }
        return messageMapper.selectCount(queryWrapper);
    }

    /**
     * 新增消息
     * @param message
     * @return
     */
    public int addMessage(Message message) {
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(SensitiveFilter.filter(message.getContent()));
        return messageMapper.insert(message);
    }

    /**
     * 读消息,修改消息状态
     * @param ids
     * @return
     */
    public int readMessage(List<Integer> ids) {
        LambdaUpdateWrapper<Message> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Message::getId, ids);
        Message message = new Message();
        message.setStatus(1);
        return messageMapper.update(message, updateWrapper);
    }

    /**
     * 查询某个主题下最新的通知
     * @param userId
     * @param topic
     * @return
     */
    public Message findLatestNotice(int userId, String topic) {
        return messageMapper.selectLatestNotice(userId, topic);
    }

    /**
     * 查询某个主题下包含的通知数量
     * @param userId
     * @param topic
     * @return
     */
    public int findNoticeCount(int userId, String topic) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Message::getStatus, 2)
                .eq(Message::getFromId, 1)
                .eq(Message::getToId, userId)
                .eq(Message::getConversationId, topic);
        return messageMapper.selectCount(queryWrapper);
    }

    /**
     * 查询未读的通知数量
     * @param userId
     * @param topic
     * @return
     */
    public int findNoticeUnreadCount(int userId, String topic) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getStatus, 0)
                .eq(Message::getFromId, 1)
                .eq(Message::getToId, userId);
        if (topic != null) {
            queryWrapper.eq(Message::getConversationId, topic);
        }

        return messageMapper.selectCount(queryWrapper);
    }


    /**
     * 查询某个主题所包含的通知列表
     * @param userId
     * @param topic
     * @param offset
     * @param limit
     * @return
     */
    public List<Message> findNotices(int userId, String topic, int offset, int limit) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Message::getStatus, 2)
                .eq(Message::getFromId, 1)
                .eq(Message::getToId, userId)
                .eq(Message::getConversationId, topic)
                .orderByDesc(Message::getCreateTime)
                .last(" limit " + offset + ", " + limit);

        return messageMapper.selectList(queryWrapper);
    }

}
